package light.head.dao.inv;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import light.head.cache.Cache;
import light.head.framework.base.BaseDAO;
import light.head.model.inv.InvCommodityInfo;

import org.springframework.stereotype.Service;

/**
 * 商品信息
 * @author yuleidian
 *
 */
@Service
public class InvCommodityInfoDAO extends BaseDAO<InvCommodityInfo, Serializable>{
	private static final String sql_loadAll = "FROM InvCommodityInfo ci WHERE ci.deleted = 0 AND ci.oc = ?";
	/**
	 * 根据oc查询所有的商品信息s
	 */
	public List<InvCommodityInfo> getAll(Integer oc) {
		if(oc == null){
			return null;
		}
		 List<InvCommodityInfo> list = Cache.commodityInfo;
		 if (null == list) {
			 list = loadAll(oc);
		 }
		 return list;
	 }
	
	// 查询所有类型，并保存其父子级关系
    public List<InvCommodityInfo> loadAll(Integer oc) {
		 List<InvCommodityInfo> list = find(sql_loadAll, oc);
		 if (null != list && list.size() > 0) {
			 for (InvCommodityInfo ci : list) {
				 for (InvCommodityInfo son: list) {
					 if (null == ci.getSonList())
						 ci.setSonList(new ArrayList<InvCommodityInfo>());
					 if (ci.getCode() == son.getPc()){
						 ci.getSonList().add(son);
					 }
				 }
			 }
				 
		 List<InvCommodityInfo> slist = new ArrayList<InvCommodityInfo>();
		 	for (InvCommodityInfo ci : list) {
				if (null == ci.getPc() || ci.getPc() == -1) {
					slist.add(ci);
				}
		 	}
			 Cache.commodityInfo = slist;
				return slist;
		 }
		 Cache.commodityInfo = null;
		 return null;
	 }
}
